﻿using Customer.Domain;
using Customer.Domain.Dto;
using Customer.Infrstructre.ClientManager;
using Customer.Infrstructre.CusContactManager;
using Customer.Read.API.Applcation.Command.ClientCommand;
using MediatR;
using Newtonsoft.Json;
using System.Diagnostics.Contracts;
using WMSpublic;
using static WMSpublic.ParseAPI;

namespace Customer.Read.API.Applcation.CommandHandler.ClientCommanfHandler
{
    public class ClientCusContactFanCommandHandler: IRequestHandler<ClientCusContactFanCommand, ClientCusContactDTO>
    {
        private readonly IClientRepository _clientRep;
        private readonly ICusContactRepository _cusContactRep;

        public ClientCusContactFanCommandHandler(IClientRepository clientRep, ICusContactRepository cusContactRep)
        {
            _clientRep = clientRep;
            _cusContactRep = cusContactRep;
        }

        public async Task<ClientCusContactDTO> Handle(ClientCusContactFanCommand request, CancellationToken cancellationToken)
        {
            List<CusContact> cusContacts = await GetCusContact();
            var list = from a in await _clientRep.GetAllAsync()
                       join b in await _cusContactRep.GetAllAsync()
                       on a.CusContactId equals b.CusContactId
                       where a.ClientId == request.ClientId
                       select new ClientCusContactDTO
                       {
                           ClientId = a.ClientId,
                           ClientCode = a.ClientCode,
                           ClientName = a.ClientName,
                           Cfax = a.Cfax,
                           Landline = a.Landline,
                           Mailbox = a.Mailbox,
                           CusContactId = a.CusContactId,
                           Cremark = a.Cremark,
                           Office_Id = a.Office_Id,
                           User_Id = a.User_Id,
                           Ccontact = b.Ccontact,
                           Caddress = b.Caddress,
                           Cphone = b.Cphone,
                           CreatTime = b.CreatTime
                       };
            return list.ToList().FirstOrDefault();
        }

        public async Task<dynamic> GetCusContact()
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIzIiwic2lkIjoiMTg1OTgwMzUwNjYzMDg1NjcwNCIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIxLDEsMSwxIiwianRpIjoiNDMzMDY2ZGYtZjI2MC00MjY3LWJhZjktYmQzODFkZWFkYzBiIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6IuWNouiKsyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6Ii8iLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.sWP96tbSYf5jz3reVpvWkhrkHgM2U5YkHFmwk4E7_V0";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string lianxi = "http://10.223.8.21:8000/ywd/api/CusContactRead/GetCusContact";
            string postReponse = await client.GetAsync(lianxi);
            var json = JsonConvert.DeserializeObject<ApiResultObjects>(postReponse);
            return json.data;
        }

        public class ApiResultObjects
        {
            public List<Customer.Domain.CusContact> data { get; set; }
        }
    }
}
